<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.428.0.min.js"></script>
<script src="${ctx}/static/js/jquery/jquery-1.8.3.min.js"></script>
<script src="${ctx}/static/js/s3.js"></script>
<script src="${ctx}/static/js/config.js"></script>
<script src="${ctx}/static/js/ws.js"></script>
<script src="${ctx}/static/js/websocket.js"></script>
<title>测试</title>
</head>

<body>
<button type="button" onclick="getcontorlts()">获取最近会话列表</button>
<button type="button" onclick="getfri()">获取好友列表</button>
clientId:<input type="text" id="clientId"  /> <br />
内容    :<input type="text" id="content"  /> <br />
消息类型:<input type="text" id="typ"  /> <br />
接收人:<input type="text" id="toId"  />
<input id="file" type="file" /><br />
<input type="button" onclick="uploadFile()" value="上传" />
<input type="button" onclick="javascript:sendMsg1()"value="发送" />
<input type="button" onclick="javascript:sendgroupMsg1()"value="发送群聊消息测试" />
<br />
接收消息:<pre id="textvalue" ></pre>


<div id="di"></div>
<div id="di1"></div>
<style>

pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
<script>
var token1 = 'da44638a-c0ea-4626-ae99-5d28ca49e573';
var userid;
var baseUrl='${ctx}';
function login(){
	$.ajax({
		  type: 'POST',
		  data:{
			  password: '123456',
			  phone: '15210399570',
		  },
		  url:baseUrl+'/u/login',
		  success: function(data,status,xhr){
			  if(data.code == 1){
				  userid = data.obj.userid;
				  userid = userid.length>1?userid:"0"+userid;
				  var xAuthToken = xhr.getResponseHeader("xAuthToken"); 
				  console.log("login xAuthToken:"+xAuthToken);
				  Config.init({
				  	baseUrl: baseUrl, 
				  	xAuthToken: xAuthToken
				  });
				  S3Util.init();
			  }else{
				  console.log("login error:"+data);
			  }
		  }
	});
}
S3Util.onerror = function(data) {
	 console.log("onerror:"+data);
}
login();

var path = "";

function uploadFile(){
	var file = document.getElementById("file").files[0];
	var url = S3Util.upload(file,"userinfo/"+userid+"/",function(err,data){
		  if (err) {
		    console.log("Error", err);
		    alert('上传失败');
		  }else{
		    console.log("Upload Success", data.Location);
		    var img ="<img src='"+data.Location+"'></img>";
		    path=data.Location;
			console.log(img);
	//		document.body.innerHTML += img;
			sendMsg2();
		  }
	});
}
function RndNum(n){
    var rnd="";
    for(var i=0;i<n;i++)
        rnd+=Math.floor(Math.random()*10);
    return rnd;
}

function test1(rid) {
	$("#clientId").val(RndNum(9))
	$("#toId").val(rid)
	$("#typ").val("1")
}
function sendMsg1(){
	var context = $("#content").val();
	var to = $("#toId").val();
	var clientid=$("#clientId").val();
	var type=$("#typ").val();
	webs.sendMsg(to,content,clientid,type);
}
function sendgroupMsg1(){
	var content = $("#content").val();
	var to = $("#toId").val();
	var clientid=$("#clientId").val();
	var type=$("#typ").val();
	webs.sendgroupMsg(to,content,clientid,type);
}
function sendMsg2(){
	var context = $("#content").val();
	var to = $("#toId").val();
	var clientid=$("#clientId").val();
	var type='2'
	webs.sendMsg(to,path,clientid,type);
}
//会话框测试
function getcontorlts() {
	$.ajax({
		  type: 'POST',
		  url: '/im-web/im/getContacts',
		  traditional: true,
		  data: {
			  xAuthToken:token1
		  },
		  success: function(data){
			  console.log(JSON.stringify(data.obj))
			  $("#di").html("");
			  $("#di").append("<h2>最近会话列表</h2>")
			  for ( var i in data.obj) {
				  $("#di").append(data.obj[i].nickname+"<button type='button' onclick='test1("+data.obj[i].rid+")' id='"+data.obj[i].rid+"' value='"+data.obj[i].rid+"'>聊天</button>")
			}
		  }
	});
}

//获取好友列表
function getfri() {
	$.ajax({
		  type: 'POST',
		  url: '/im-web/im/getofroster',
		  traditional: true,
		  data: {
			  xAuthToken:token1
		  },
		  success: function(data){
			  $("#di1").html("");
			  $("#di1").append("<h2>好友列表</h2>")
			  for ( var i in data.obj) {
				  var sp="";
				  var sp1 = data.obj[i].jid;
				  sp = sp1;
				  $("#di1").append(data.obj[i].nickName+"<button type='button' onclick='test1("+sp+")' id='"+sp+"' value='"+sp+"'>聊天</button>")
				   console.log(JSON.stringify(data.obj))
			  }
		  }
	});
}


</script>

</body>
</html>